package com.atguigu.eduservice.service.impl;

import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.CourseInfoForm;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQueryVo;
import com.atguigu.eduservice.entity.vo.CourseWebVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-06
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
   @Autowired
   private EduCourseDescriptionService courseDescriptionService;
   @Autowired
   private EduChapterService eduChapterService;
   @Autowired
   private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;
   //添加课程相关信息
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {

        //1、保存课程基本信息
        EduCourse eduCourse = new EduCourse();
        //将前端传过来的数据复制到educourse中，只要两个类中的字段名一致即可
        //字段多的时候使用BeanUtils比较方便
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        //2、判断是否保存成功
        if(insert!=1){
            throw new GuliException(20001,"添加课程相关信息失败");
        }
        //3、获取课程id
        String courseId=eduCourse.getId();
        //4、保存描述信息
        EduCourseDescription courseDescription=new EduCourseDescription();
        courseDescription.setId(courseId);
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescriptionService.save(courseDescription);
        return courseId;
    }
//根据课程id查询课程相关信息
    @Override
    public CourseInfoForm getCourseInfoById(String courseId) {
        //1、查询课程信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //2、查询课程描述信息
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        //3、封装数据
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse,courseInfoForm);
        courseInfoForm.setDescription(courseDescription.getDescription());
        //4、返回数据
        return courseInfoForm;
    }
//修改课程相关信息
    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //1、保存课程基本信息
        EduCourse eduCourse = new EduCourse();
        //将前端传过来的数据复制到eduCourse中，只要两个类中的字段名一致即可
        //字段多的时候使用BeanUtils比较方便
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        //2、判断是否保存成功
        if(update!=1){
            throw new GuliException(20001,"修改课程相关信息失败");
        }
        //3、获取课程id
        String courseId=eduCourse.getId();
        //4、保存描述信息
        EduCourseDescription courseDescription=new EduCourseDescription();
        courseDescription.setId(courseId);
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescriptionService.updateById(courseDescription);

    }

//根据课程id查询课程发布相关信息
    @Override
    public CoursePublishVo getCoursePublishById(String id) {
        CoursePublishVo coursePublishVo=baseMapper.getCoursePublishById(id);
        return coursePublishVo;
    }

//根据课程id删除课程相关信息
    @Override
    public void delCourseInfoById(String courseId) {
        // 删除视频
        //1.1查询小节信息
        QueryWrapper<EduVideo> videoIdWrapper = new QueryWrapper<>();
        videoIdWrapper.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(videoIdWrapper);
        //1.2遍历集合取出videoId
        List<String>videoIdList=new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            //1.3封装videoId
            EduVideo eduVideo = videoList.get(i);
            videoIdList.add(eduVideo.getVideoSourceId());
        }
        //1.4判断集合
        if(videoIdList.size()>0){
            vodClient.delVideoList(videoIdList);
        }
        //删除小节
        QueryWrapper<EduVideo>videoWrapper=new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        eduVideoService.remove(videoWrapper);
        //删除章节
        QueryWrapper<EduChapter>chapterWrapper=new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        eduChapterService.remove(chapterWrapper);
        //删除描述
        courseDescriptionService.removeById(courseId);
        //删除课程
        int delete = baseMapper.deleteById(courseId);
        if(delete!=1){
            throw new GuliException(20001,"删除课程失败");
        }
    }
//前台带条件分页查询课程信息
    @Override
    public Map<String, Object> getCourseApiPageVo(Page<EduCourse> pageParam, CourseQueryVo courseQueryVo) {
        //1.从CourseQueryVo获取参数
        String subjectParentId = courseQueryVo.getSubjectParentId();
        String subjectId = courseQueryVo.getSubjectId();
        String buyCountSort = courseQueryVo.getBuyCountSort();
        String gmtCreateSort = courseQueryVo.getGmtCreateSort();
        String priceSort = courseQueryVo.getPriceSort();
        //2.参数验空，拼写查询条件
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","Normal");
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(buyCountSort)) {
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(gmtCreateSort)) {
            queryWrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(priceSort)) {
            queryWrapper.orderByDesc("price");
        }
        //3.分页查询
        baseMapper.selectPage(pageParam, queryWrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        //4.封装数据
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
//前台根据课程id查询课程详情信息
    @Override
    public CourseWebVo getCourseWebVo(String id) {
        CourseWebVo courseWebVo=baseMapper.getCourseWebVo(id);
        return courseWebVo;
    }
}
